package com.yzh.openai.trigger.http;

import com.alibaba.fastjson.JSON;
import com.yzh.openai.domain.auth.model.entity.AuthStateEntity;
import com.yzh.openai.domain.auth.model.entity.UserEntity;
import com.yzh.openai.domain.auth.model.valobj.AuthTypeVO;
import com.yzh.openai.domain.auth.service.IAuthService;
import com.yzh.openai.domain.weixin.model.entity.MessageTextEntity;
import com.yzh.openai.domain.weixin.model.entity.UserBehaviorMessageEntity;
import com.yzh.openai.domain.weixin.model.valobj.MsgTypeVO;
import com.yzh.openai.domain.weixin.service.IWeiXinBehaviorService;
import com.yzh.openai.types.common.Constants;
import com.yzh.openai.types.common.Response;
import com.yzh.openai.types.utils.xml.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

/**
 * 鉴权控制器
 */
@Slf4j
@RestController
@CrossOrigin("${app.config.cross-origin}")
@RequestMapping("/api/${app.config.api-version}/auth/")
public class AuthController {
    @Resource
    private IAuthService authService;
    @Resource
    private IWeiXinBehaviorService weiXinBehaviorService;

    @PostMapping("gencode")
    public Response<String> genCode(@RequestParam String openid) {
        UserBehaviorMessageEntity userBehaviorMessageEntity = new UserBehaviorMessageEntity();
        userBehaviorMessageEntity.setOpenId(openid);
        userBehaviorMessageEntity.setMsgType(MsgTypeVO.TEXT.getCode());
        userBehaviorMessageEntity.setContent("666");
        String xml = weiXinBehaviorService.acceptUserBehavior(userBehaviorMessageEntity);
        MessageTextEntity messageTextEntity = XmlUtil.xmlToBean(xml, MessageTextEntity.class);
        return Response.<String>builder()
                .code(Constants.ResponseCode.SUCCESS.getCode())
                .info(Constants.ResponseCode.SUCCESS.getInfo())
                .data(messageTextEntity.getContent())
                .build();
    }

    @PostMapping("/login")
    public Response<String> doLogin(@RequestParam String code) {
        log.info("鉴权登录校验开始，验证码：{}", code);
        try {
            AuthStateEntity authStateEntity = authService.doLogin(code);
            log.info("鉴权登录校验完成，验证码：{} 结果：{}", code, JSON.toJSONString(authStateEntity));
            //拦截，鉴权失败
            if (!AuthTypeVO.A0000.getCode().equals(authStateEntity.getCode())) {
                return Response.error(Constants.ResponseCode.TOKEN_ERROR);
            }
            //放行，鉴权成功
            return Response.success(authStateEntity.getToken());

        } catch (Exception e) {
            log.error("鉴权登录校验失败，验证码：{}", code);
            return Response.error();
        }
    }

    @GetMapping("/userInfo")
    public Response<UserEntity> queryUserInfo(@RequestHeader("Authorization") String token) {
        try {
            UserEntity userEntity = authService.queryUserInfo(token);
            return Response.success(userEntity);
        } catch (Exception e) {
            return Response.error();
        }
    }
}
